package com.pipodi.itasamobile.parsers;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.pipodi.itasamobile.R;
import com.pipodi.itasamobile.connections.Connection;
import com.pipodi.itasamobile.model.Subtitle;
import com.pipodi.itasamobile.model.interfaces.SubtitleInterface;
import com.pipodi.itasamobile.utils.SubtitlesListAdapter;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class SubtitlesParser extends AsyncTask<String, String, String> {

	private List<SubtitleInterface> items;
	private ListView listView;
	private Context context;
	private LayoutInflater inflater;
	private String name;
	private View parent;
	
	private boolean tvSeriesFound = true;

	public SubtitlesParser(ListView view, Context context,
			LayoutInflater inflater, String name, View parent) {
		super();
		this.listView = view;
		this.items = new ArrayList<SubtitleInterface>();
		this.context = context;
		this.inflater = inflater;
		this.name = name;
		this.parent = parent;
	}

	@Override
	protected String doInBackground(String... params) {
		Document doc = Connection
				.connectToAPIUrl("https://api.italiansubs.net/api/rest/shows/search?q="
						+ this.name
						+ "&apikey=ce0a811540244ad5267785731a5c37a0");
		NodeList subList = doc.getElementsByTagName("data");
		Node subNode = subList.item(0);
		Element eElement2 = (Element) subNode;
		if (Integer.parseInt(eElement2.getElementsByTagName("count").item(0)
				.getTextContent()) == 0) {
			this.tvSeriesFound = false;
			return null;
		}
		subList = doc.getElementsByTagName("show");
		subNode = subList.item(0);
		eElement2 = (Element) subNode;
		int show_id = Integer.parseInt(eElement2.getElementsByTagName("id")
				.item(0).getTextContent());
		doc = Connection
				.connectToAPIUrl("https://api.italiansubs.net/api/rest/subtitles/search?q="
						+ this.name
						+ "&show_id="
						+ show_id
						+ "&apikey=ce0a811540244ad5267785731a5c37a0");
		Log.e("URL", "https://api.italiansubs.net/api/rest/subtitles/search?q="
				+ this.name + "&show_id=" + show_id
				+ "&apikey=ce0a811540244ad5267785731a5c37a0");
		NodeList nList3 = doc.getElementsByTagName("subtitle");
		for (int temp = 1; temp < 11; temp++) {
			Node nNode = nList3.item(temp);
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {
				Element eElement = (Element) nNode;
				String show_name = eElement.getElementsByTagName("show_name")
						.item(0).getTextContent();
				String version = eElement.getElementsByTagName("version")
						.item(0).getTextContent();
				String episode = episodeNumberParser(eElement
						.getElementsByTagName("name").item(0).getTextContent(),
						show_name);
				int sub_id = Integer.parseInt(eElement.getElementsByTagName("id").item(0).getTextContent());
				SubtitleInterface sub = new Subtitle(sub_id, show_name,
						episode, version);
				this.items.add(sub);
				Log.e("SUB", sub.toString());
			}
		}
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		SubtitlesListAdapter adapter = new SubtitlesListAdapter(context,
				inflater, items);
		listView.setAdapter(adapter);
		TextView warning = (TextView) parent.findViewById(R.id.warningTextSub);
		if (tvSeriesFound == false){
			warning.setText("Nessuna serie TV trovata!");
		}
		else{
			warning.setText("");
		}
		Log.e("SUBTITLES", "Adapter inizializzato");
	}

	private String episodeNumberParser(String str, String name) {
		String temp = str.replace(name + " ", "");
		return temp;
	}
}
